<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AdminUser extends Model
{
    protected $table = 'admin_user';
    protected $primaryKey = 'id';

    protected $fillable = [
    	'user_name', 'phone', 'email', 'password', 'is_super', 'status', 'description'
    ];
    protected $hidden = [
        'password',
    ];
    protected $appends = [
        'role_title', 'role_ids'
    ];

    /**
     * 获得此用户的角色
     * @return [type] [description]
     */
    public function roles()
    {
        return $this->belongsToMany('App\Models\AdminRole','admin_user_role','admin_user_id','admin_role_id');
    }

    public function getRoleTitleAttribute()
    {
        $role_title = '';
        $roles = $this->roles()->where('status', 1)->get()->toArray();

        foreach ($roles as $role) {
            $role_title .= $role['title'] . '、';
        }

        return trim($role_title, '、');
    }

    public function getRoleIdsAttribute()
    {
        return $this->roles()->pluck('id');
    }
}
